<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class Issue extends Model
{
    protected $table='issues';

    protected $fillable = ['journal_id', 'title', 'title_en', 'year', 'volume', 'issue', 'page_start', 'page_end', 'journal_created', 'journal_published', 'zr_id'];
    public function journal()
    {
        return $this->belongsTo(Journal::class);
    }

    public function currentIssue()
    {
    	return (new static())
    		->orderBy('journal_published', 'desc')
    		->orderBy('id', 'desc')
    		->first();
    }
    
    public function getTitle()
    {
        return $this->title_en ? $this->title_en : $this->title;
    }

    public function formatIssueText()
    {
        return $this->year . ' Vol. ' . $this->volume . ' (' . $this->issue . '): ' . $this->page_start . '-' . $this->page_end;
    }

    public function getIssue($id)
    {
        return $this->where('id', $id)->first();
    }

    public function articles()
    {
        return $this->hasMany(Article::class);
    }

    public function getPrevId()
    {
        $id = $this->id;
        return $this->where('id', '<', $id)->max('id');
    }

    public function getNextId()
    {
        $id = $this->id;
        return  $this->where('id', '>', $id)->min('id');
    }

    public function isIncoming()
    {
        return $this->journal_published > date('Y-m-d');
    }
}